﻿Imports MySql.Data.MySqlClient.MySqlParameter

Public Class AlarmCheckForm

    Private _offset As UShort = 7033
    Private _netid As Byte

    Private Sub AlarmCheckForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _netid = DevTypeService.GetMinDevId(MainForm.pi.ProjectID)
        LoadData()
#If LSC_PROJECTTYPE_SWITCH = "SALES" Then
        Me.btnSaveLightAlarm.Visible = False
#End If
    End Sub

    Private Sub LoadData()
        '读取参数(电话号码和电话自检间隔时间)
        Dim buf() As Short

        'buf = COMHelper.Instance.Send4Short(g_netID, FunctionCode.WordWritableRead, _offset, 1)
        buf = COMHelper.Instance.Send4Short(_netid, FunctionCode.WordWritableRead, _offset, 1)


        If buf Is Nothing Then
            Me.btnSaveLightAlarm.Enabled = False
            msg.ShowError(g_strReadError)
            Exit Sub
        End If

        If (buf(0) = 1) Then
            optStartLightAlarm.Checked = True
        Else
            optStopLightAlarm.Checked = True
        End If

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
        'Dim frm As New AlarmCheckViewForm
        'frm.Show()
    End Sub

#Region "保存"

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If InsertRecord(Me.chkLight.Checked, Me.chkPhone.Checked, Me.txtPhone1.Text) Then
            'Me.btnSave.Enabled = False
            msg.Show("保存成功")
        Else
            msg.ShowError("保存失败")
        End If
    End Sub

    Private Function InsertRecord(ByVal resultLight As Boolean, ByVal resultPhone As Boolean, ByVal strPhone As String) As Boolean
        Dim strSql As String
        strSql = "Insert Into tbccCheckAlarm(projectID,checkTime,UserName,resultLight,resultPhone,phoneNumber) Values(?p0,?p1,?p2,?p3,?p4,?p5)"

        Dim p(5) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
            p(i).ParameterName = "p" + i.ToString
        Next
        p(0).ParameterName = "p0" : p(0).Value = MainForm.pi.ProjectID
        p(1).ParameterName = "p1" : p(1).Value = Now
        p(2).ParameterName = "p2" : p(2).Value = TBCC.UserManage.User.Instance.uName
        p(3).ParameterName = "p3" : p(3).Value = IIf(resultLight, 1, 0)
        p(4).ParameterName = "p4" : p(4).Value = IIf(resultPhone, 1, 0)
        p(5).ParameterName = "p5" : p(5).Value = strPhone

        Return 1 = SqlHelper.ExecuteNonQuery(strSql, p)
    End Function

#End Region


    Private Sub SetContolState(ByVal b As Boolean)
        Me.btnSave.Enabled = b
        Me.btnSaveLightAlarm.Enabled = b
        Me.btnStartPhoneAlarm.Enabled = b
    End Sub

    Private Sub btnSaveLightAlarm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveLightAlarm.Click
        Dim value As Short
        If Me.optStartLightAlarm.Checked Then
            value = 1
        Else
            value = 0
        End If

        SetContolState(False)
        Dim result As Boolean
        'result = COMHelper.Instance.Write(g_netID, FunctionCode.WordWrite, _offset, value)
        result = COMHelper.Instance.Write(_netid, FunctionCode.WordWrite, _offset, value)
        SetContolState(True)

        If result Then
            msg.Show("设置成功")
        Else
            msg.ShowError("通讯失败")
        End If
    End Sub

    

    ''' <summary>
    ''' 判断用户输入数据的合法性
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckValida() As Boolean
        If IsDigital(Me.txtPhone1.Text) = False Then
            msg.ShowWarning("电话号码只能是数字")
            Me.txtPhone1.Focus()
            Return False
        End If

        Return True
    End Function

    ''' <summary>
    ''' 判断字符串是否为数字字符串
    ''' </summary>
    ''' <param name="str"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function IsDigital(ByVal str As String) As Boolean
        str = str.Trim()
        For i As Integer = 0 To str.Length - 1
            Dim c As Char
            c = str.Substring(i)
            If c = "," OrElse c = "#" Then
                Continue For
            End If
            If Not Char.IsDigit(c) Then
                Return False
            End If
        Next

        Return True
    End Function

 
    Private Sub btnStartPhoneAlarm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartPhoneAlarm.Click
        Dim strPhone As String
        strPhone = Me.txtPhone1.Text.Trim()

        If strPhone = "" Then
            msg.Show("请输入电话号码")
            Me.txtPhone1.Focus()
            Exit Sub
        End If
        If strPhone.Length > 16 Then
            msg.Show("输入电话号码应小于16位！")
            Me.txtPhone1.Focus()
            Exit Sub

        End If

        Dim cmd As String
        cmd = String.Format("AT^DIALTEST={0}", strPhone)

        SetContolState(False)
        Dim result As String
        result = COMHelper.Instance.WriteCommand(cmd)
        SetContolState(True)

        If String.IsNullOrEmpty(result) Then
            msg.ShowError("发送命令后,没有收到响应,请重试")
            Exit Sub
        End If

        If result.IndexOf("OK") >= 0 Then
            msg.Show("命令发送成功")
        Else
            msg.ShowError("正在拨号中")
        End If
 
    End Sub
End Class


